package com.hxzy.research.controller.adminresearch;

import com.hxzy.research.pojo.TblBranch;
import com.hxzy.research.pojo.TblUser;
import com.hxzy.research.service.pageandmanager.AdminUserLogin;
import com.hxzy.research.service.research.AddUserBranch;
import com.hxzy.research.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserManagerController {
    //调用登录验证的接口
    @Autowired private AdminUserLogin adminUserLogin;
    @Autowired private AddUserBranch addUserBranch;
    /**
     * 分公司账号管理
     */
    @RequestMapping("/admin/userManager.go")
    public String userManager(Model model) throws Exception{
        List<TblUser> userList=adminUserLogin.findUserList();
        model.addAttribute("userList",userList);
        return "/admin/research/userManager";
    }
    //跳转到分公司添加页面
    @RequestMapping("/admin/goAddUser.go")
    public String goAddUser() throws Exception{
        return "/admin/research/addUserManager";
    }
    //分公司添加
    @RequestMapping("/admin/addUser.go")
    public @ResponseBody ResultVo addUser(TblUser tblUser)throws Exception{
        //第一步先添加branch表--通过tblUser得到名字
        TblBranch tblBranch=new TblBranch();
        tblBranch.setBranchname(tblUser.getComment());
        addUserBranch.addBranch(tblBranch);
        //通过名字查询到新添加的分公司的id
        TblBranch tblBranch1=addUserBranch.findBranchidByName(tblBranch.getBranchname());
        //执行User表添加
        tblUser.setBranchid(tblBranch1.getId());
        addUserBranch.addUser(tblUser);
        ResultVo resultVo=new ResultVo("200","添加成功");
        return resultVo;
    }

    //跳转到分公司修改页面
    @RequestMapping("/admin/goUpdateUser.go")
    public String goUpdateUser(Model model,Integer userId, HttpSession session)throws Exception{
        //通过userId查到要修改的信息用户
        TblUser tblUser=addUserBranch.findUserByUserId(userId);
        session.setAttribute("branchid",tblUser.getBranchid());
        model.addAttribute("user",tblUser);
        return "/admin/research/updateUserManager";
    }
    //分公司修改
    @RequestMapping("/admin/updateUser.go")
    public @ResponseBody ResultVo updateUser(TblUser tblUser,HttpSession session)throws Exception{
        //执行修改--先修改user表
        addUserBranch.updateUser(tblUser);
        //执行修改-再修改branch表
        TblBranch tblBranch=new TblBranch();
        tblBranch.setId(tblUser.getBranchid());
        tblBranch.setBranchname(tblUser.getComment());
        addUserBranch.updateBranch(tblBranch);
        ResultVo resultVo=new ResultVo("200","修改成功");
        return resultVo;
    }

    //分公司删除
    @RequestMapping("/admin/deleteUser.go")
    public String deleteUser(Integer userid,HttpSession session)throws Exception{
        //执行删除branch和tbluser表之前，需要先通过userid查到branchid
        TblUser tblUser=addUserBranch.findUserByUserId(userid);
        //执行删除-先删user表
        addUserBranch.deleteUser(userid);

        //执行删除-再删除branch表-用user表的branchid来删除

        addUserBranch.deleteBranch(tblUser.getBranchid());
        return "redirect:/admin/userManager.go";
    }

}
